package com.nchu.controller;

import com.nchu.VO.ProductVO;
import com.nchu.VO.UserVO;
import com.nchu.common.util.PageResult;
import com.nchu.common.util.R;
import com.nchu.entity.Product;
import com.nchu.entity.User;
import com.nchu.service.IProductService;
import com.nchu.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yaojie
 * @since 2025-01-09
 */
@RestController
@RequestMapping("/product")
@CrossOrigin //配置跨域
public class ProductController {
    @Autowired
    private IProductService productService;
    @GetMapping("/list")
    public R list(){
        List<Product> productList = productService.list();
        return R.ok().data("productList",productList);
    }
    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize) {
        PageResult<Product> pr = productService.findByPage(page,pageSize);
        //返回结果
        return R.ok().data("pr",pr);
    }
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false)ProductVO productVO){
        PageResult<Product> pr = productService.search(page,pageSize,productVO);
        return R.ok().data("pr",pr);
    }
    //添加菜品信息
    @PostMapping("/addProduct")
    public R addStudent(@RequestBody Product product){
        boolean f = productService.addProduct(product);
        return f ? R.ok().message("菜品信息添加成功!") : R.error().message("菜品信息添加失败!");
    }
    //更新菜品信息
    @PostMapping("/updateProduct")
    public R updateStudent(@RequestBody Product product){
        boolean f = productService.updateById(product);
        return f ? R.ok().message("修改菜品信息成功!") : R.error().message("修改菜品信息失败!");
    }
    //删除菜品信息这里的id可以填多个id如/1,3,4
    @PostMapping("/deleteProduct/{id}")
    public R delete(@PathVariable("id") String id){
        String[] split = id.split(",");
        List<String> productIds = Arrays.asList(split);
        return productService.removeByIds(productIds) ? R.ok().message("删除菜品信息成功!") : R.error().message("删除菜品信息失败!");
    }
}

